
package com.wutianyi.maven.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * @author wutianyi
 *         配置文件的路径只支持classpath下面
 */
public class ParserXmlDom4j {

	private static Logger		logger						= Logger.getLogger(ParserXmlDom4j.class);
	/**
	 * 默认定义的config配置文件
	 */
	private final static String	DEFAULT_CONFIG_PATH			= "autoconfig/autoconfig.xml";
	private static final String		PROPERTY_PARENT_TAG_NAME	= "properties";
	private static final String		PROPERTY_TAG_NAME			= "property";
	private static final String		GENERATOR_PARENT_TAG_NAME	= "generators";
	private static final String		GENERATOR_TAG_NAME			= "generator";

	private Document			document;

	/**
	 *用户可以自己指定config配置文件的路径
	 */
	private String				configPath;

	/**
	 * 定义的属性
	 */
	private Map<String, String>	properties;
	/**
	 * 需要渲染的模板
	 */
	private Map<String, String>	generatorFiles;

	/**
	 * 对字段进行的解释
	 */
	private Map<String, String>	descriptions;

	/**
	 * 
	 * @param path
	 * @param isFile 为true的时候表示采用的是加载文件的方式，为false表示采用classpath的方式加载文件
	 * 对于对war包进行相应的autoconfig的时候，需要制定文件的路径
	 */
	public ParserXmlDom4j(String path, boolean isFile) {

		this.configPath = path;
		InputStream inputStream = null;
		
		if(isFile) {
			try {
				inputStream = new FileInputStream(new File(path));
			}catch(FileNotFoundException e) {
				e.printStackTrace();
			}
		} else {
			inputStream = this.getClass().getResourceAsStream("/" + configPath);
		}
		init(inputStream);
	}

	public ParserXmlDom4j() {

		this(DEFAULT_CONFIG_PATH, false);
	}

	public ParserXmlDom4j(InputStream inputStream) {

	}

	/**
	 * 初始化properties，generatorFiles解析config xml文件
	 */
	private void init(InputStream inputStream) {

		// 说明没有定义autoconfig xml文件，则不继续进行初始化

		if(null == inputStream) {
			logger.info("The autoconfig.xml is not exist!");
			return;
		}
		SAXReader reader = new SAXReader();
		properties = new HashMap<String, String>();
		generatorFiles = new HashMap<String, String>();
		descriptions = new HashMap<String, String>();
		try {
			document = reader.read(inputStream);
			Element root = document.getRootElement();

			Element propertiesElement = root.element(PROPERTY_PARENT_TAG_NAME);

			for(Iterator<Element> itr = propertiesElement.elementIterator(PROPERTY_TAG_NAME); itr.hasNext();) {
				Element e = itr.next();
				properties.put(e.attributeValue("name"), e.attributeValue("default-value"));
				descriptions.put(e.attributeValue("name"), e.attributeValue("description"));
			}
			Element generators = root.element(GENERATOR_PARENT_TAG_NAME);
			for(Iterator<Element> itr = generators.elementIterator(GENERATOR_TAG_NAME); itr.hasNext();) {
				Element e = itr.next();
				generatorFiles.put(e.attributeValue("source"), e.attributeValue("dest"));
			}
		}catch(DocumentException e) {
			e.printStackTrace();
		}
	}

	public Map<String, String> getProperties() {

		return properties;
	}

	public void setProperties(Map<String, String> properties) {

		this.properties = properties;
	}

	public Map<String, String> getGeneratorFiles() {

		return generatorFiles;
	}

	public Map<String, String> getDescriptions() {

		return descriptions;
	}

	public void setDescriptions(Map<String, String> descriptions) {

		this.descriptions = descriptions;
	}

	public void setGeneratorFiles(Map<String, String> generatorFiles) {

		this.generatorFiles = generatorFiles;
	}

	public static void main(String[] args) {

		System.out.println(System.getProperty("user.home"));
	}
}
